package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.example.common.Result;
import com.example.dao.BookDao;
import com.example.dao.StudyDao;
import com.example.entity.*;
import com.example.service.ReservebookService;
import com.example.service.ReservestudyService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 日期：2025/5/11 17:55
 */

@CrossOrigin
@RestController
@RequestMapping("/reservestudy")
public class ReserveStudyController {

    @Resource
    private ReservestudyService reservestudyService;

    @Resource
    private StudyDao studyDao;

    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Reservestudy> info = reservestudyService.findBySearch(params);
        return Result.success(info);
    }

    // 新增或修改
    @PostMapping
    public Result save(@RequestBody Reservestudy reservestudy){
        Study study = studyDao.selectByPrimaryKey(reservestudy.getStudyId());

        if (study.getNum() == 0){
            return Result.error("数量不能为0");
        }

        reservestudy.setTime(DateUtil.now());
        reservestudyService.add(reservestudy);

        study.setNum(study.getNum() - 1);
        studyDao.updateByPrimaryKey(study);

        return Result.success();
    }



    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        reservestudyService.delete(id);
        return Result.success();
    }
}
